<?php

namespace app\controller\v1\admin;

use app\BaseController;
use app\common\Send;
use app\service\MenuService;
use think\App;

class Menu extends BaseController
{
    use Send;
    protected $menuService;

    public function __construct(App $app, MenuService $menuService)
    {
        parent::__construct($app);
        $this->menuService = $menuService;
    }

    // 获取菜单列表
    public function index()
    {
        $data = $this->request->param();
        $menus = $this->menuService->list($data);
        return self::success($menus);
    }

    // 显示创建菜单的表单
    public function save()
    {
        $data = $this->request->post();
        $menu = $this->menuService->createMenu($data);
        // 这里可以返回一个视图或JSON响应，用于显示创建菜单的表单
        return self::success('Create form');
    }

    // 显示指定菜单
    public function show($id)
    {
        $menu = $this->menuService->getMenuById($id);
        if (!$menu) {
            return self::error('Menu not found', 404);
        }
        return self::success($menu);
    }

    // 显示编辑菜单的表单
    public function edit($id)
    {
        $menu = $this->menuService->getMenuById($id);
        if (!$menu) {
            return Send::error('Menu not found', 404);
        }
        // 这里可以返回一个视图或JSON响应，用于显示编辑菜单的表单
        return self::success($menu, 'Edit form');
    }

    // 更新指定菜单
    public function update($id)
    {
        $data = $this->request->put();
        $menu = $this->menuService->updateMenu($id, $data);
        if (!$menu) {
            return self::error('Menu not found', 404);
        }
        return self::success($menu, 'Menu updated successfully');
    }

    // 删除指定菜单
    public function delete($id)
    {
        $result = $this->menuService->deleteMenu($id);
        if (!$result) {
            return self::error('Menu not found', 404);
        }
        return self::success(null, 'Menu deleted successfully');
    }
}
